home *** CD-ROM | disk | FTP | other *** search
/ Disc to the Future 2 / Disc to the Future Part II Programmer's Reference (Wayzata Technology)(6013)(1992).bin / MAC / THINKC / 3_0 / CUTILSLI / UTILITYL / HELP.C < prev    next >
Text File  |  1987-11-10  |  13KB  |  448 lines

  1. /**************************************************************************
  2. *    Filename:    Help.c                                                                 
  3. *    Purpose:    Online Help with the List Manager                      
  4. *    Authors:    Robert E. Neville from William Rausch's MacTutor example
  5. *                of April 1987.                                                      
  6. *    Date:        November 5, 1987                                                        
  7. *    Functions:     do_help(),         read_help(),
  8. *                show_help(),     help_filter(), 
  9. *                help_action(),    FileError().
  10. *    Version:    1.0                                                                            
  11. *    Copyright:    Hummingbird Graphics & Software                                  
  12. ***************************************************************************/
  13.  
  14. #include <EventMgr.h>
  15. #include <WindowMgr.h>
  16. #include <ToolboxUtil.h>
  17. #include <DialogMgr.h>
  18. #include <FontMgr.h>
  19. #include <ListMgr.h>
  20. #include <strings.h>
  21. #include <pascal.h>
  22.  
  23. #define HELPDLOG    301            /* Dialog RSRC for Help */
  24. #define HELPOK        1            /* OK Button in HELPDLOG */
  25. #define HELPBOX        3            /* Help List box RSRC */
  26. #define HELPLIST    2            /* Where the Help Goes RSRC */
  27. #define MAXHELPS    100            /* Maximum Help Items */
  28. #define ACTIVATE    0
  29. #define INACTIVATE    255
  30.  
  31. /* These must be redeclared elsewhere */
  32.  
  33. #define HELPSTR        600            /* STR# RSRC for Help*/
  34. #define ERRORALERT  400            /* Error Alert RSRC */
  35.  
  36. struct
  37. {
  38.     Rect            text_box;            /* User item dimensions */
  39.     Rect            topic_box;            /* User item dimensions */
  40.     TEHandle        text;                /* Text Edit Handle */
  41.     ListHandle        topics;                /* List Handle */
  42.     Rect            d_rect;                /* Destination Rect */
  43.     Rect            v_rect;                /* View Rect */
  44.     int                offset;                /* d_rect Vertical shift */
  45.     int                lines_vis;            /* Number of lines visible */
  46.     ControlHandle    text_scroll;        /* Control Handle */
  47.     int                max_text;            /* Max value of scroller */
  48.     int                help_id[MAXHELPS];    /* Topics' HELP ID's */
  49.     int                num_topics;            /* Number of topics */
  50.     int                last_one;            /* Last cell clicked */
  51. } helplist;
  52.  
  53. pascal Boolean help_filter();
  54. Boolean           show_help();
  55. pascal void       help_action();
  56.  
  57.  
  58. /****************************************************************
  59. *    Function:    do_help(str_id)
  60. *    Purpose:    Present user with List  of Topics and help
  61. *                text about each one. The Topics are stored
  62. *                as a STR# resource and the help texts are
  63. *                stored as HELP resources. Each topic
  64. *                string contains the number of its associated
  65. *                Help Resource. This function is the visible one.
  66. *    Passed:        str_id    -    short
  67. *    Returned:    Nothing.                                        
  68. *****************************************************************/
  69.  
  70. do_help(str_id)
  71. short        str_id;
  72. {
  73.     DialogPtr    the_dialog;
  74.     Handle        scr_handle;
  75.     short        scratch;
  76.     Str255        scr_str;
  77.     Rect        hdata_rect;
  78.     Point        cell_size;
  79.     Handle        topics;
  80.     GrafPtr        old_port;
  81.     char        *Ferror = "returned an Error";
  82.     
  83.     topics = GetResource('STR#', str_id);
  84.     helplist.num_topics = *(short *)(*topics);
  85.     the_dialog = GetNewDialog(HELPDLOG, 0L, -1L);
  86.     GetPort(&old_port);
  87.     SetPort(the_dialog);
  88.     GetDItem(the_dialog, 
  89.              HELPLIST, 
  90.              &scratch, 
  91.              &scr_handle, 
  92.              &helplist.topic_box);
  93.     InsetRect(&helplist.topic_box, 1, 1);
  94.     helplist.topic_box.right -= 15;
  95.     SetRect(&hdata_rect,0,0,1,helplist.num_topics);
  96.     SetPt(&cell_size, helplist.topic_box.right - helplist.topic_box.left, 16);
  97.     helplist.topics = LNew(&helplist.topic_box, 
  98.                          &hdata_rect, 
  99.                          cell_size,
  100.                          0,
  101.                          the_dialog,
  102.                          FALSE,
  103.                          FALSE,
  104.                          FALSE,
  105.                          TRUE);
  106.     (*helplist.topics)->selFlags = lOnlyOne;
  107.     InsetRect(&helplist.topic_box, -1, -1);
  108.     read_help(helplist.topics,helplist.num_topics,helplist.help_id,str_id);
  109.     if ( !show_help(the_dialog,helplist.topics,helplist.help_id) )
  110.         FileError("\pshow_help(), ", Ferror);
  111.     SetPort(old_port);
  112. }                                /* End of do_help */
  113.             
  114. /************************************************************
  115. *    Function:    read_help(topics,num_topics,help_id,str_id)
  116. *    Purpose:    Read the topics from STR# RSRC. Add them to
  117. *                the List as they are read. Also save the ID's
  118. *                in an array for use later in finding the 
  119. *                proper HELP RSRC to display for each topic.
  120. *    Passed:        topics        -    List Handle
  121. *                num_topics    -    SHORT
  122. *                help_id        -    Ptr to SHORT array
  123. *                str_id        -    SHORT
  124. *    Returned:    Nothing.                                        
  125. *************************************************************/
  126.  
  127. read_help(topics,num_topics,help_id,str_id)
  128. ListHandle        topics;
  129. short            num_topics;
  130. short            help_id[];
  131. short            str_id;
  132. {
  133.     short        i;
  134.     Str255    a_topic;
  135.     Point    the_cell;
  136.     char    *id_number;
  137.     Str255    strvar;
  138.     
  139.     for ( i = 0; i < num_topics; i++ )
  140.     {
  141.         GetIndString(a_topic,str_id, i + 1);
  142.         PtoCstr(a_topic);
  143.         id_number = strchr(a_topic, (char)'\\');
  144.         *id_number++ = '\0';
  145.         help_id[i] = atoi(id_number);
  146.         SetPt(&the_cell, 0, i);
  147.         LSetCell(a_topic,strlen(a_topic), the_cell, topics);
  148.     }
  149.     SetPt(&the_cell, 0, 0);
  150.     LSetSelect((Boolean)TRUE, the_cell, topics);
  151.     LDoDraw((Boolean)TRUE, topics);
  152. }
  153.  
  154. /************************************************************
  155. *    Function:    show_help(the_dialog,topics,help_id)
  156. *    Purpose:    Set up the help text for the first topic in
  157. *                the List. Call ModalDialog (with a filterproc)
  158. *                to do all the work.  Loop until OK clicked.
  159. *    Passed:        the_dialog    -    Dialog Ptr
  160. *                topics        -    List Handle
  161. *                help_id        -    Ptr to SHORT array
  162. *    Returned:    TRUE                                        
  163. *************************************************************/
  164.  
  165. Boolean show_help(the_dialog,topics,help_id)
  166. DialogPtr    the_dialog;
  167. ListHandle    topics;
  168. short        help_id[];
  169. {
  170.     GrafPtr    old_port;
  171.     short    item_hit;
  172.     short    scr_int;
  173.     Handle    scr_handle;
  174.     Boolean    done = FALSE;
  175.     short    i, buf_size;
  176.     Rect    scroll_rect;
  177.     Handle    the_help;
  178.     
  179.     GetDItem(the_dialog, HELPBOX, &scr_int, &scr_handle, &helplist.text_box);
  180.     helplist.text_box.right -= 16;
  181.     scroll_rect.right = helplist.text_box.right + 15;
  182.     scroll_rect.left = helplist.text_box.right - 1;
  183.     scroll_rect.top = helplist.text_box.top;
  184.     scroll_rect.bottom = helplist.text_box.bottom;
  185.     helplist.text_scroll = NewControl(the_dialog, 
  186.                                     &scroll_rect,
  187.                                     "",
  188.                                        TRUE,
  189.                                        0,
  190.                                        0,
  191.                                        0,
  192.                                        16,
  193.                                        0L);
  194.     HiliteControl(helplist.text_scroll, INACTIVATE);
  195.     helplist.d_rect.top = helplist.text_box.top + 1;
  196.     helplist.d_rect.left = helplist.text_box.left + 1;
  197.     helplist.d_rect.right = helplist.text_box.right - 1;
  198.     helplist.d_rect.bottom = 20000;
  199.     helplist.v_rect = helplist.text_box;
  200.     InsetRect(&helplist.v_rect, 1, 1);
  201.     helplist.text = TENew(&helplist.d_rect, &helplist.v_rect);
  202.     (*helplist.text)->txFont = systemFont;
  203.     (*helplist.text)->txSize = 12;
  204.     helplist.last_one = 0;
  205.     helplist.offset = 0;
  206.     helplist.lines_vis = (helplist.v_rect.bottom - helplist.v_rect.top) 
  207.                         / (*helplist.text)->lineHeight;
  208.     the_help = GetResource('HELP',help_id[0]);
  209.     buf_size = SizeResource(the_help);
  210.     TEDeactivate(helplist.text);
  211.     TESetSelect(32767L,32767L,helplist.text);
  212.     HLock(the_help);
  213.     TEInsert(*the_help,(long)buf_size,helplist.text);
  214.     HUnlock(the_help);
  215.     if ( (*helplist.text)->nLines > helplist.lines_vis )
  216.     {
  217.         HiliteControl(helplist.text_scroll, ACTIVATE);
  218.         helplist.max_text = (((*helplist.text)->nLines) 
  219.                         - helplist.lines_vis) 
  220.                         * (*helplist.text)->lineHeight;
  221.         SetCtlMax(helplist.text_scroll, helplist.max_text);
  222.     }
  223.     ShowWindow(the_dialog);
  224.     do
  225.     {
  226.         ModalDialog(help_filter, &item_hit);
  227.         if (item_hit == HELPOK)
  228.             done = TRUE;
  229.     } while (!done);
  230.     TEDispose(helplist.text);
  231.     LDispose(helplist.topics);
  232.     DisposDialog(the_dialog);
  233.     return(TRUE);
  234. }                                /* end of show_help */
  235.         
  236. /************************************************************
  237. *    Function:    help_filter(dp,ep,ip)
  238. *    Purpose:    Handle the activate and updating of the
  239. *                scroller and the box area.  Handle mouseDowns
  240. *                in the scroller and text box, returning
  241. *                TRUE, and returning FALSE for everything else
  242. *                so the Dialog Manager does not abort. 
  243. *    Passed:        dp        -    Window Ptr
  244. *                ep        -    Ptr to Event Record
  245. *                ip        -    Ptr to SHORT
  246. *    Returned:    TRUE    -    Mouse Downs in Scroller, text box
  247. *                FALSE    -    Everything else                                        
  248. *************************************************************/
  249.     
  250. pascal Boolean help_filter(dp,ep,ip)
  251. WindowPtr    dp;
  252. EventRecord    *ep;
  253. short        *ip;
  254. {
  255.     short            part;
  256.     ControlHandle    ch;
  257.     char            tempchar;
  258.     Point            mouse_loc;
  259.     short            delta;
  260.     long            a_cell;
  261.     short            cell_num;
  262.     Handle            the_help;
  263.     short            buf_size;
  264.     short            scr_int;
  265.     Handle            scr_handle;
  266.     Rect            scr_rect;
  267.     
  268.     switch (ep->what)
  269.     {
  270.         case updateEvt:
  271.             if (ep->message == (long)dp)
  272.             {
  273.                 GetDItem(dp,HELPOK,&scr_int,&scr_handle,&scr_rect);
  274.                 InsetRect(&scr_rect, -4,-4);
  275.                 PenSize(3,3);
  276.                 FrameRoundRect(&scr_rect, 16,16);
  277.                 PenNormal();
  278.                 FrameRect(&helplist.topic_box);
  279.                 FrameRect(&helplist.text_box);
  280.                 EraseRect(&helplist.v_rect);
  281.                 TEUpdate(&helplist.v_rect, helplist.text);
  282.                 LUpdate(dp->visRgn, helplist.topics);
  283.             }
  284.             return (FALSE);
  285.         case keyDown:
  286.             tempchar = BitAnd(ep->message, charCodeMask);
  287.             if (tempchar == '\r')
  288.             {
  289.                 *ip = HELPOK;
  290.                 return (TRUE);
  291.             }
  292.             return (FALSE);
  293.         case mouseDown:
  294.             mouse_loc = ep->where;
  295.             GlobalToLocal(&mouse_loc);
  296.             part = FindControl(mouse_loc, dp, &ch);
  297.             if (part > 0)
  298.             {
  299.                 if (ch == helplist.text_scroll)
  300.                 {
  301.                     if (part == inThumb)
  302.                     {
  303.                         if (TrackControl(ch, mouse_loc, 0L))
  304.                         {
  305.                             delta = helplist.offset - 
  306.                                     GetCtlValue(helplist.text_scroll);
  307.                             TEScroll(0,delta, helplist.text);
  308.                             helplist.offset -= delta;
  309.                         }
  310.                     }
  311.                     else
  312.                         TrackControl(ch,mouse_loc,help_action);
  313.                     return (TRUE);
  314.                 }
  315.                 else if (ch == (*helplist.topics)->vScroll)
  316.                 {
  317.                     LClick(mouse_loc,ep->modifiers,helplist.topics);
  318.                     return (TRUE);
  319.                 }
  320.                 else
  321.                     return (FALSE);
  322.             }
  323.             else if (PtInRect(mouse_loc, *helplist.topics))
  324.             {
  325.                 LClick(mouse_loc,ep->modifiers, helplist.topics);
  326.                 a_cell = 0;
  327.                 if (LGetSelect(TRUE, &a_cell, helplist.topics))
  328.                     cell_num = HiWord(a_cell);
  329.                 if (cell_num >= 0 && cell_num != helplist.last_one)
  330.                 {
  331.                     TEDeactivate(helplist.text);
  332.                     TESetSelect(0L,32767L,helplist.text);
  333.                     TEDelete(helplist.text);
  334.                     if (cell_num < helplist.num_topics)
  335.                     {
  336.                         the_help = GetResource('HELP', 
  337.                                                helplist.help_id[cell_num]);
  338.                         buf_size = SizeResource(the_help);
  339.                     }
  340.                     else
  341.                         buf_size = 0;
  342.                     if (buf_size > 0)
  343.                     {
  344.                         HLock(the_help);
  345.                         TEInsert(*the_help,(long)buf_size,helplist.text);
  346.                         HUnlock(the_help);
  347.                     }
  348.                     SetCtlValue(helplist.text_scroll,0);
  349.                     delta = helplist.offset - GetCtlValue(helplist.text_scroll);
  350.                     TEScroll(0,delta,helplist.text);
  351.                     helplist.offset -= delta;
  352.                     if ((*helplist.text)->nLines > helplist.lines_vis)
  353.                     {
  354.                         HiliteControl(helplist.text_scroll,ACTIVATE);
  355.                         helplist.max_text = (((*helplist.text)->nLines) 
  356.                                         - helplist.lines_vis)
  357.                                         * (*helplist.text)->lineHeight;
  358.                         SetCtlMax(helplist.text_scroll,helplist.max_text);
  359.                     }
  360.                     else
  361.                         HiliteControl(helplist.text_scroll,INACTIVATE);
  362.                     helplist.last_one = cell_num;
  363.                 }
  364.                 return (TRUE);
  365.             }
  366.             return (FALSE);
  367.         default:
  368.             return (FALSE);
  369.     }
  370. }                                /* end of help_filter */
  371.  
  372. /************************************************************
  373. *    Function:    help_action(the_scroll,partcode)
  374. *    Purpose:    Routine called by Toolbox during TrackControl.
  375. *                Takes care of scrolling the text when the 
  376. *                up/down arrow and page parts of scroll bar
  377. *                are clicked in. 
  378. *    Passed:        the_scroll    -    Control Handle
  379. *                partcode    -    short
  380. *    Returned:    Nothing                                        
  381. *************************************************************/
  382.  
  383. pascal void help_action(the_scroll,partcode)
  384. ControlHandle    the_scroll;
  385. short            partcode;
  386. {
  387.     short    value, delta;
  388.     
  389.     switch (partcode)
  390.     {
  391.         case inUpButton:
  392.             value = GetCtlValue(the_scroll);
  393.             value = (value - (*helplist.text)->lineHeight > 0)
  394.                    ? value - (*helplist.text)->lineHeight : 0;
  395.             SetCtlValue(the_scroll,value);
  396.             break;
  397.         case inPageUp:
  398.             value = GetCtlValue(the_scroll);
  399.             value = (value - 6 * (*helplist.text)->lineHeight > 0)
  400.                    ? value - 6 * (*helplist.text)->lineHeight : 0;
  401.             SetCtlValue(the_scroll,value);
  402.             break;
  403.         case inDownButton:
  404.             value = GetCtlValue(the_scroll);
  405.             value = (value + (*helplist.text)->lineHeight < helplist.max_text)
  406.                    ? value + (*helplist.text)->lineHeight : helplist.max_text;
  407.             SetCtlValue(the_scroll,value);
  408.             break;
  409.         case inPageDown:
  410.             value = GetCtlValue(the_scroll);
  411.             value = (value + 6 * (*helplist.text)->lineHeight 
  412.                    < helplist.max_text)
  413.                    ? value + 6 * (*helplist.text)->lineHeight 
  414.                    : helplist.max_text;
  415.             SetCtlValue(the_scroll,value);
  416.             break;
  417.     }
  418.     delta = helplist.offset - GetCtlValue(helplist.text_scroll);
  419.     TEScroll(0,delta,helplist.text);
  420.     helplist.offset -= delta;
  421. }                                /* End of help_action */
  422.  
  423. /****************************************************************
  424. *    Function:    FileError(str)
  425. *    Purpose:    Issue Alert if Standard File goes Wrong.
  426. *    Passed:        str1,str2    -    Pascal String Ptr.
  427. *    Returned:    Nothing.
  428. *****************************************************************/
  429.  
  430. FileError(str1,str2)
  431. char    *str1;                                    /* Ptr to String */    
  432. char    *str2;                
  433. {
  434.     ParamText(str1, str2,"\p","\p");            /* pass string to ParamText */
  435.     NoteAlert(ERRORALERT, 0L);                    /* Issue Error Alert */
  436. }                                                /* End of FileError */
  437.  
  438. /**********     End of file     **********/
  439.                   
  440.                   
  441.                   
  442.                   
  443.                   
  444.                   
  445.                   
  446.                   
  447.                   
  448.